package com.jframe.cfg.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jframe.cfg.model.Fun;
import com.jframe.cfg.model.Iframe;
import com.jframe.cfg.model.Rolefun;
import com.jframe.cfg.model.Rolemenu;
import com.jframe.cfg.model.Sysmenu;
import com.jframe.cfg.service.FunService;
import com.jframe.cfg.service.IframeService;
import com.jframe.cfg.service.RolefunService;
import com.jframe.cfg.service.RolemenuService;
import com.jframe.cfg.service.SysmenuService;
import com.jframe.sso.model.Role;
import com.jframe.sso.service.RoleService;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.SpringUtil;
import com.jframe.sys.utils.StringUtil;
import com.jframe.sys.webui.Tree;

public class RolefunAction extends ExtendAction {

	public void getRightTrees(){
		int groupId = StringUtil.parseInt(this.request.getParameter("groupId"));
		RoleService roleService = (RoleService)SpringUtil.getBean("roleService");
		List<Role> treels = roleService.findByGroupId(groupId);
		List<Tree> ls = new ArrayList<Tree>();
		for(Role role:treels){
			Tree tree = new Tree();
			tree.setId(""+role.getId());
			tree.setText(role.getName());						
			ls.add(tree);
		}
		this.print(JsonUtil.list2Json(ls));
	}
	
	public void Iframelist(){
		int roleId = StringUtil.parseInt(request.getParameter("roleId"));
		//RolefunService rolefunService = (RolefunService) this.getBean("rolefunService");
		RolemenuService rolemenuService = (RolemenuService)this.getBean("rolemenuService");
		IframeService iframeService = (IframeService)this.getBean("iframeService");
		//获取该角色下的所有菜单
		Rolemenu rolemenu = rolemenuService.load("roleId", roleId);
		SysmenuService sysmenuService = (SysmenuService)this.getBean("sysmenuService");
		List<Sysmenu> sysmenus = sysmenuService.findListByIds(rolemenu.getMenuIds());
		//获取该菜单下的全部叶签,
		List<Iframe> iframes = iframeService.findListByMenuCodes(this.getMenuCodes(sysmenus));
		//装载成ztree
		List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
		for(Iframe iframe:iframes){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("id", iframe.getId());
			map.put("name", iframe.getTitle());
			items.add(map);
		}
		this.print(JsonUtil.list2Json(items));
	}
	
	public void getIframeList(){
		int roleId = StringUtil.parseInt(request.getParameter("roleId"));
		RoleService roleService = (RoleService)this.getBean("roleService");
		IframeService iframeService = (IframeService)this.getBean("iframeService");
		RolemenuService rolemenuService = (RolemenuService)this.getBean("rolemenuService");
		Role role = roleService.load(roleId);
		Rolemenu rolemenu = rolemenuService.load("roleId", roleId);
		SysmenuService sysmenuService = (SysmenuService)this.getBean("sysmenuService");
		List<Sysmenu> list = sysmenuService.findListByUpIdAndGroupId(1,role.getGroupId());
		List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
		for(Sysmenu menu:list){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("id", menu.getId());
			map.put("text", menu.getText());
			map.put("children", iframeService.findListMapByproperties(menu.getId(), rolemenu.getMenuIds()));
			items.add(map);
		}
		this.print(JsonUtil.list2Json(items));
	}
	
	public void funlist(){
		int roleId = StringUtil.parseInt(request.getParameter("roleId"));
		int iframeId = StringUtil.parseInt(request.getParameter("iframeId"));
		RolefunService rolefunService = (RolefunService) this.getBean("rolefunService");
		FunService funService = (FunService)this.getBean("funService");
		Rolefun roleFun = rolefunService.getRolefunByProperties(roleId,iframeId);
		List<Fun> funs = funService.findListByIframeId(iframeId);
		List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
		for(Fun fun:funs){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("id", fun.getId());
			map.put("name", fun.getName());
			if(isIn(roleFun.getFunIds(),fun.getId())){
				map.put("checked", true);
			}else{
				map.put("checked", false);
			}
			items.add(map);
		}
		String json = JsonUtil.list2Json(items);
		this.print(json);	
	}
	
	public void save(){
		int roleId = StringUtil.parseInt(this.request.getParameter("roleId"));
		int iframeId = StringUtil.parseInt(this.request.getParameter("iframeId"));
		String ids = StringUtil.null2Space(this.request.getParameter("ids"));
		RolefunService rolefunService = (RolefunService) this.getBean("rolefunService");
		Rolefun roleFun = rolefunService.getRolefunByProperties(roleId, iframeId);
		roleFun.setFunIds(ids);
		roleFun.setRoleId(roleId);
		roleFun.setIframeId(iframeId);
		rolefunService.save(roleFun);
		this.print(true);
	}
	
	private boolean isIn(String funIds,int funId){
		boolean flag = false;
		String[] ids =  StringUtil.null2Space(funIds).split(",");
		for(String id:ids){
			if(id.equals(""+funId)){
				flag = true;
				break;
			}
		}
		return flag;
	}
	
	private String getMenuCodes(List<Sysmenu> sysmenus){
		String codes = "";
		for(Sysmenu menu:sysmenus){
			codes +=menu.getCode()+",";
		}
		if(sysmenus.size()>0){
			codes = codes.substring(0,codes.length()-1);
		}
		return codes;
	} 
	
}
